$(document).ready(function () {
    initUser();
})
//init()

var $loginModal = $('#MyLoginModal');
var $username = $loginModal.find('#username');
var $password = $loginModal.find('#password');
var $repassword=$loginModal.find('#repassword').parents('div').eq(1);
var $registerWarn=$('#Registerwarn').parents('div').eq(1);
var $repeatname=$('#repeatname').parents('div').eq(1);
var $Login=$('#login');
var $Register=$('#register');
var username;
var userid
var status; //记录是注册还是登录  登录1 注册0

function CloseModal()
{
    Modalempty();
    $loginModal.modal('hide');
    $repeatname.addClass('hide');
}
function Modalempty() {
    $username.val("");
    $password.val("");
    $repassword.val("");
}

function initUser()
{
    $.ajax({
        url:url+"getSession.do",
        type:"get",
        datatype:"json",
        data:"",
        success:function (data) {
            var data = JSON.parse(data);
            if(data.result=="success")
            {
                var user=data.user;
                username=user.username;
                userid=user.id;
                LoginSuccess(username)
            }
        }
    })
}

//跨域只能用get   不能用post
$('#login').on('click',function () {
    var json={username:$username.val(), password:$password.val()}
    $.ajax({
        url:url+"loginvalidate.do",
        type:"get",
        datatype:"json",
        data:json,
        success:function(data){
             var data=JSON.parse(data);
             if(data.result=="success")
             {
                 //测试成功
                 var user=JSON.parse(data.user);
                 userid=user.id;
                 username=user.username;
                 LoginSuccess(user.username);
                 window.location.reload();

             }
             else{
                 alert("登录失败");
             }

        }
    })
})

function LoginSuccess(username) {
    CloseModal();
    $('.title li[name="Login"]').addClass("hide");
    $('.title li[name="LoginSucess"]').removeClass("hide");
    $('.title li[name="LoginSucess"] span[name="username"]').text(username);
}

function logout() {
    $('.title li[name="Login"]').removeClass("hide");
    $('.title li[name="LoginSucess"]').addClass("hide");
    $.ajax({
        url:url+'deleteSession.do',
        data:"",
        type:"get",
        success:function (data) {
            window.location.reload();
        }
    })
    username=undefined;
    userid=undefined;
}
function initLogin() {
    $repassword.addClass("hide");
    $Register.addClass("hide");
    $Login.removeClass("hide");
    $registerWarn.addClass('hide');
    status=1;
}
function initRegister()
{
    $Login.addClass("hide");
    $repassword.removeClass("hide");
    $Register.removeClass("hide");
    $registerWarn.addClass('hide');

    status=0;
}
$Register.on('click',function(){
    $registerWarn.addClass('hide');
   if($password.val()!=$('#repassword').val())
   {
       $registerWarn.removeClass("hide")
       $('#Registerwarn').text("两次输入密码不一致")
       $repassword.focus()
   }
   else
   {
       var json={username:$username.val(), password:$password.val()}
        $.ajax({
            url:url+'register.do',
            type:"get",
            datatype:"json",
            data:json,
            success:function (data) {
                data = JSON.parse(data);
                if(data.result=="error")
                {
                    $repeatname.removeClass('hide');
                    $('#repeatname').text("用户名重复");
                }
                else
                {
                    CloseModal();
                }
            }
        })
   }
})
/*
* 登录、注销、注册的全部在上面写
* */

